package com.hxzy.controller.common;

import com.hxzy.common.constant.Constants;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.utils.text.Convert;
import com.hxzy.common.utils.uuid.UUID;
import com.hxzy.system.service.SysConfigService;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 生成验证码类说明
 *
 * @author admin
 * @date 2021-09-01
 */
@RestController
public class CaptchaController extends BaseController {

    @Autowired
    private StringRedisTemplate  stringRedisTemplate;

    @Autowired
    private SysConfigService sysConfigService;



    @GetMapping ("/captcha/gif")
    public AjaxResult gifCaptcha(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "width",required = false,defaultValue= "150") int width,
                                 @RequestParam(value = "height",required = false,defaultValue= "50") int height,
                                 @RequestParam(value = "len",required = false,defaultValue= "4") int len) throws Exception {

        // 使用gif验证码
        GifCaptcha gifCaptcha = new GifCaptcha(width,height,len);
        String imageStr=gifCaptcha.toBase64();

        //存储到session中，供下次验证时候使用
//        HttpSession session=request.getSession();
//        session.setAttribute("captchaValue", gifCaptcha.text().toLowerCase());
        //前后端分离，session无法共用，只能redis缓存来操作
        String uuid=UUID.randomUUID().toString();
        String redisKey= Constants.CAPTCHA_CODE_KEY+ uuid;
        this.stringRedisTemplate.opsForValue().set(redisKey,gifCaptcha.text().toLowerCase(),15L, TimeUnit.MINUTES);

        //发送一份给前端 (图片，验证码redis的key)
        AjaxResult  ajaxResult=AjaxResult.success();
        ajaxResult.put("key",uuid);
        ajaxResult.put("img",imageStr);
        String value=this.sysConfigService.selectValueByKey("sys.account.captchaOnOff");
        ajaxResult.put("captchaOnOff",Convert.toBool(value,true));

        return ajaxResult;
    }

    @GetMapping ("/captcha/png")
    public AjaxResult pngCaptcha(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(value = "width",required = false,defaultValue= "150") int width,
                           @RequestParam(value = "height",required = false,defaultValue= "50") int height,
                           @RequestParam(value = "len",required = false,defaultValue= "4") int len) throws Exception {

        SpecCaptcha captcha = new SpecCaptcha(width, height);
        String imageStr=captcha.toBase64();
        captcha.setLen(len);
        String uuid=UUID.randomUUID().toString();
        String redisKey= Constants.CAPTCHA_CODE_KEY+ uuid;
        this.stringRedisTemplate.opsForValue().set(redisKey,captcha.text().toLowerCase(),15L, TimeUnit.MINUTES);

        //发送一份给前端 (图片，验证码redis的key)
        AjaxResult  ajaxResult=AjaxResult.success();
        ajaxResult.put("key",uuid);
        ajaxResult.put("img",imageStr);
        String value=this.sysConfigService.selectValueByKey("sys.account.captchaOnOff");
        ajaxResult.put("captchaOnOff",Convert.toBool(value,true));
        return ajaxResult;
    }


    @GetMapping ("/captcha/arith")
    public AjaxResult arithmeticCaptcha(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(value = "width",required = false,defaultValue= "150") int width,
                           @RequestParam(value = "height",required = false,defaultValue= "50") int height,
                           @RequestParam(value = "len",required = false,defaultValue= "2") int len) throws Exception {

        ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height);
        captcha.setLen(len);
        String imageStr=captcha.toBase64();
        String uuid=UUID.randomUUID().toString();
        String redisKey= Constants.CAPTCHA_CODE_KEY+ uuid;
        this.stringRedisTemplate.opsForValue().set(redisKey,captcha.text().toLowerCase(),15L, TimeUnit.MINUTES);

        //发送一份给前端 (图片，验证码redis的key)
        //发送一份给前端 (图片，验证码redis的key)
        AjaxResult  ajaxResult=AjaxResult.success();
        ajaxResult.put("key",uuid);
        ajaxResult.put("img",imageStr);
        String value=this.sysConfigService.selectValueByKey("sys.account.captchaOnOff");
        ajaxResult.put("captchaOnOff",Convert.toBool(value,true));
        return ajaxResult;
    }

}
